package com.xpgk.mvc.ddc.zcdj.ywsl;

import java.io.File;
import java.io.IOException;

import org.apache.tomcat.util.http.fileupload.FileUtils;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.Ret;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.upload.UploadFile;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.tools.PathFileKit;

@Service(name = VehicleService.serviceName)
public class VehicleService extends BaseService {
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(VehicleService.class);
	public static final String serviceName = "vehicleService";
	
	// 设置用户头像
	static public Ret setVehiclePhoto(String IDS, String CLXH, UploadFile file) {
		Ret ret = new Ret();
		String fileName = file.getFileName();
		File uploadFile = file.getFile();
		String extension = fileName.substring(fileName.lastIndexOf("."));
		if (".png".equals(extension) || ".jpg".equals(extension) || ".gif".equals(extension) || "jpeg".equals(extension) || "bmp".equals(extension)) {
			// 使用缩略图
//				fileName = new ImageKit().thumbnailImage(uploadFile, 150, 150, "thumb_", false);
//				File source = new File(fileName);
			
			String savePath = PathKit.getWebRootPath() + "/carPhoto/"+CLXH;
			String saveFileName = CLXH + extension;
			try {
				File p = new File(savePath);
				if (!p.exists())
					p.mkdirs();
					PathFileKit.copyFile(uploadFile, new File(savePath + "/" + saveFileName));
					ret.set("flag", 1);
			} catch (IOException e1) {
				e1.printStackTrace();
				ret.set("msg", "文件写入服务器出现错误，请稍后再上传");
				ret.set("flag", 0);
			}
//				source.delete();
			String src = "upload/images/userPhoto/" + CLXH + "/" + saveFileName;
			ret.set("src", src); // 相对地址，显示图片用
//				int userID = UserService.getUser(userName).getInt("id");
			Db.update("update DDC_VEHICLE set PHOTO=? where IDS=?", src, IDS);
		} else {
			ret.set("msg", "只允许上传png,jpg,jpeg,gif,bmp类型的图片文件");
			ret.set("flag", 0);
		}
//		uploadFile.deleteOnExit();
		uploadFile.delete();
		return ret;
	}
}
